package com.wuyi.config;

import com.wuyi.controller.interceptor.UserLoginInterceptor;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName WebMvcConfig.java
 * @Description
 * @createTime 2021年08月02日 13:28:00
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * Add handlers to serve static resources such as images, js, and, css
     * files from specific locations under web application root, the classpath,
     * and others.
     *
     * @param registry
     * 实现静态资源映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/META-INF/resources/"); //映射swagger2
//                .addResourceLocations("file:D:\\JavaWorkspace\\workspaces\\images\\");//映射本地静态资源
    }

    @Bean
    public UserLoginInterceptor userLoginInterceptor() {
        return new UserLoginInterceptor();
    }

    /**
     * 注册拦截器
     * @param registry
     * @return
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userLoginInterceptor())
//                .addPathPatterns("/hello")//拦截的
                .addPathPatterns("/post/*")
                .addPathPatterns("/follow/*")
                .addPathPatterns("/collect/*")
                .addPathPatterns("/center/*")//个人中心
                .excludePathPatterns("/index/*").excludePathPatterns("/post/details");//不拦截的
        WebMvcConfigurer.super.addInterceptors(registry);
    }






    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}
